Hema kalyani(You)
Message yourself
Today
704 Binary Search9:37 pm
9:37 pm
class Solution {
public:
int trap(vector<int>& height) {
int left = 0;
int right = height.size() - 1;
int leftMax = 0, rightMax = 0;
int waterTrapped = 0;
while (left < right) {
if (height[left] < height[right]) {
if (height[left] >= leftMax)
leftMax = height[left];
else
waterTrapped += leftMax - height[left];
left++;
} else {
if (height[right] >= rightMax)
rightMax = height[right];
else
waterTrapped += rightMax - height[right];
right--;
}
}
return waterTrapped;
}
};11:15 pm
11:15 pm
42 Trapping Rain Water11:15 pm
11:15 pm
W
Binary Search .docx
DOCX•7 kB•
11:21 pm
W
Linked list cycle.docx
DOCX•7 kB•
11:22 pm
W
Trapping rain water.docx
DOCX•7 kB•
11:22 pm
W
Linked list cycle.docx
DOCX•7 kB•
11:25 pm
PDF
Linked list cycle.pdf
1 page•PDF•29 kB•
11:30 pm